//
//  ViewController1.m
//  循环引用示例
//
//  Created by 江湖 on 2019/6/14.
//  Copyright © 2019 江湖. All rights reserved.
//

#import "ViewController1.h"
#import "TestTool.h"
@interface ViewController1 ()<TestToolDelegate>

@property (nonatomic,strong)TestTool *tool;
@property (nonatomic,strong) NSTimer *timer;
@end

@implementation ViewController1

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithTitle:@"返回" style:UIBarButtonItemStyleDone target:self action:@selector(back)];
    
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    button.frame = CGRectMake(20, 100, 100, 30);
    [button setTitle:@"跳转" forState:0];
    [button setTitleColor:[UIColor yellowColor] forState:0];
    [button addTarget:self action:@selector(buttonClick) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:button];
    self.tool = [[TestTool alloc]init];
//    1协议强引用
//    self.tool.delegate = self;
//    2父子类造成强引用
    self.tool.ID = self;//
//    3block强引用
    __weak typeof(self) weakSelf = self;
//    [self.tool testBlock:^(NSString *string) {
//        NSLog(@"%@",string);
//        self.view.backgroundColor = [UIColor whiteColor];
//    }];
//    4 NSTimer强引用
//    self.timer = [NSTimer scheduledTimerWithTimeInterval:3 target:weakSelf selector:@selector(timerClick) userInfo:nil repeats:YES];
//    不会强引用
//    self.timer = [NSTimer scheduledTimerWithTimeInterval:1 repeats:YES block:^(NSTimer * _Nonnull timer) {
//        weakSelf.view.backgroundColor = [UIColor yellowColor];
//    }];
    // Do any additional setup after loading the view from its nib.
}

- (void)timerClick{
    NSLog(@"我在重复");
}


//ViewController1协议
- (void)protacolTest:(NSString *)string{
    NSLog(@"%@",string);
}

-(void)back{
    if ([self.delegate respondsToSelector:@selector(protacolTest:)]) {
        [self.delegate protacolTest:@"测试协议"];
    }
    [self.navigationController popViewControllerAnimated:YES];
}



//是否调用dealloc方法，没有则造成了循环引用
- (void)dealloc {
    NSLog(@"ViewController1被销毁了");
//    [self.timer invalidate];
}

-(void)buttonClick {
    ViewController1 *vc = [[ViewController1 alloc]init];
    [self.navigationController pushViewController:vc animated:YES];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
